package com.asset.dispoal.channel.service.common;

import com.asset.dispoal.channel.domain.db.entity.SysChannelPay;
import com.asset.dispoal.channel.domain.db.mapper.SysChannelMapper;
import com.asset.dispoal.channel.dto.SysChannelPayRequest;
import com.asset.dispoal.channel.service.converter.SysChannelConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.math.BigDecimal;

/**
 * @author qr
 * @date 2022/3/25$
 */
@Component
public class SysChannelPayUpdateCommand implements BaseCommand<SysChannelPayRequest, Result> {

    @Autowired
    SysChannelMapper sysChannelMapper;

    @Override
    public Result execute(SysChannelPayRequest request) {
        SysChannelPay pay = sysChannelMapper.getByPayId(request.getId());
        SysChannelPay sysChannelDebt = SysChannelConverter.INSTANCE.resPayToDo(request);
        sysChannelDebt.setCreateTime(DateTimeUtil.currentDate());
        sysChannelDebt.setUpdateTime(DateTimeUtil.currentDate());
        sysChannelMapper.updatePay(sysChannelDebt);
        sysChannelMapper.addPayMoney(sysChannelDebt.getMoney().subtract(pay.getMoney()), pay.getChannelId());
        sysChannelMapper.addSurplus(BigDecimal.ZERO.subtract(sysChannelDebt.getMoney().subtract(pay.getMoney())), pay.getOpenId());
        return Result.ok();
    }
}
